/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import entities.Article;
import entities.Category;
import entities.UserProject;
import interfaces.ArticleInterface;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Thibault
 */
@ManagedBean
@ViewScoped
public class SubmitArticleBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @EJB
    ArticleInterface articleEJB;
    private String name;
    private String description;
    private double price;
    private Date endSale;
    private List<String> selectedCategories;

    public void createArticle() {

        FacesContext fc = FacesContext.getCurrentInstance();
        UserProject up = ((HeaderBean) fc.getExternalContext().getSessionMap().get("headerBean")).getLoggedUser();

        Article a = new Article(name, description, price, endSale, up);
        for (String c : selectedCategories) {
            a.getCategories().add(Category.valueOf(c));
        }
        articleEJB.createArticle(a);

        name = "";
        description = "";
        price = 0;
        endSale = null;
        selectedCategories.clear();
        FacesMessage msg = new FacesMessage("New auction created.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public Category[] getCategoriesValue() {
        return Category.values();
    }

    public String getName() {
        return name;
    }

    public void setName(String Name) {
        this.name = Name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String Description) {
        this.description = Description;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Date getEndSale() {
        return endSale;
    }

    public void setEndSale(Date endSale) {
        this.endSale = endSale;
    }

    public Date getMinDate() {
        return new Date();
    }

    public List<String> getSelectedCategories() {
        return selectedCategories;
    }

    public void setSelectedCategories(List<String> selectedCategories) {
        this.selectedCategories = selectedCategories;
    }
}
